'use strict';
angular.module('iframe.service',[])
    .service('ResourceVisitService',['$http','$q',function ($http,$q){

  var resourceVisitContext = "http://192.168.1.104:18086/resources";
    return{
        resourceMapping :{},
        getResourceService:function (resource){
            if(!resource){
                return null;
            }
            if(!this.resourceMapping[resource]){

                this.resourceMapping[resource]=function(){
                    var resourceVisitAddress= resourceVisitContext + '/' + resource;
                    var  resourceDataStruct=null;
                    return {

                            getPageCommandUrl: function () {
                                return resourceVisitAddress + '?command=PAGE';
                            },
                            getResourceVisitUrl: function () {
                                return resourceVisitAddress;
                            },
                            loadDataStruct: function () {
                                var deferred = $q.defer();
                                if (resourceDataStruct) {
                                    deferred.resolve(resourceDataStruct);
                                } else {
                                    var resourceVisitDataStructAddress = resourceVisitAddress + "?command=DATASTRUCT";
                                    $http({
                                        method: 'POST',
                                        url: resourceVisitDataStructAddress
                                    }).success(function (data, status, headers, config) {
                                        if (data.status == 1) {
                                            resourceDataStruct = data.result;
                                            deferred.resolve(resourceDataStruct);
                                        } else {
                                            deferred.reject(data.errorMessage);
                                        }
    // 当相应准备就绪时调用
                                    }).error(function (data, status, headers, config) {
                                        deferred.reject('http.status='+status);
    // 当响应以错误状态返回时调用
                                    });
                                }
                                return deferred.promise;
                            },
                            translateList:function(paramMap){
                                var deferred = $q.defer();
                                var resourceVisitDataStructAddress = resourceVisitAddress;
                                var searchObj = {
                                    paramsMap:paramMap
                                };
                                $http({
                                    method: 'POST',
                                    url: resourceVisitDataStructAddress,
                                    data:searchObj
                                }).success(function (data, status, headers, config) {
                                    if (data.status == 1) {
                                        var dataList = data.result;
                                        deferred.resolve(dataList);
                                    } else {
                                        deferred.reject(data.errorMessage);
                                    }
                                    // 当相应准备就绪时调用
                                }).error(function (data, status, headers, config) {
                                    deferred.reject('http.status='+status);
                                    // 当响应以错误状态返回时调用
                                });
                                return deferred.promise;
                            },
                            search:function(searchObj){
                                var deferred = $q.defer();
                                $http({
                                    method: 'POST',
                                    url: resourceVisitAddress,
                                    data:searchObj
                                }).success(function (data, status, headers, config) {
                                    if (data.status == 1 && data.result ) {
                                        if(data.result instanceof Array && data.result.length==0){
                                            deferred.reject(data.errorMessage);
                                        }else{
                                            var dataModel = data.result;
                                            deferred.resolve(dataModel);
                                        }
                                    } else {
                                        deferred.reject(data.errorMessage);
                                    }
                                    // 当相应准备就绪时调用
                                }).error(function (data, status, headers, config) {
                                    deferred.reject('http.status='+status);
                                    // 当响应以错误状态返回时调用
                                });
                                return deferred.promise;
                            },
                            visitProcess:function(paramsMap){
                                var searchObj = {
                                    paramsMap:paramsMap
                                };
                                return this.search(searchObj);
                            },
                            queryList:function(paramsMap,options){
                                var searchObj = {
                                    command:'query',
                                    resultType:'LISTMAP',
                                    paramsMap:paramsMap
                                };
                                if(options){
                                    searchObj = angular.extend(searchObj,options);
                                }
                                return this.search(searchObj);
                            },
                            queryOne:function(paramsMap){
                                var searchObj = {
                                    command:'query',
                                    resultType:'ONEMAP',
                                    paramsMap:paramsMap
                                };
                                return this.search(searchObj);
                            },
                            inheritedList:function(paramsMap,queryObj){
                                var searchObj = {
                                    command:'query',
                                    resultType:'INHERITEDLIST'
                                };
                                searchObj = angular.extend(searchObj,queryObj);
                                searchObj.paramsMap = paramsMap;
                                return this.search(searchObj);
                            },
                            loadResourceById:function (resourceId){
                                var deferred = $q.defer();
                                var resourceVisitByIdAddress = resourceVisitAddress+"/"+resourceId;
                                $http({
                                    method: 'POST',
                                    url: resourceVisitByIdAddress
                                }).success(function (data, status, headers, config) {
                                    if (data.status == 1) {
                                        var dataModel = data.result;
                                        deferred.resolve(dataModel);
                                    } else {
                                        deferred.reject(data.errorMessage);
                                    }
                                    // 当相应准备就绪时调用
                                }).error(function (data, status, headers, config) {
                                    deferred.reject('http.status='+status);
                                    // 当响应以错误状态返回时调用
                                });
                                return deferred.promise;
                            },
                            saveResource:function (resourceModel){
                                var deferred = $q.defer();

                                $http({
                                    method: 'POST',
                                    url: resourceVisitAddress,
                                    data:{paramsMap: resourceModel,command:'INSERT'}
                                }).success(function (data, status, headers, config) {
                                    if (data.status == 1) {
                                        var dataModel = data.result;
                                        deferred.resolve(dataModel);
                                    } else {
                                        deferred.reject(data.errorMessage);
                                    }
                                    // 当相应准备就绪时调用
                                }).error(function (data, status, headers, config) {
                                    deferred.reject('http.status='+status);
                                    // 当响应以错误状态返回时调用
                                });
                                return deferred.promise;
                            },
                            updateResource:function (resourceModel){
                                var deferred = $q.defer();

                                $http({
                                    method: 'POST',
                                    url: resourceVisitAddress,
                                    data:{paramsMap: resourceModel,command:'UPDATE'}
                                }).success(function (data, status, headers, config) {
                                    if (data.status == 1) {
                                        var dataModel = data.result;
                                        deferred.resolve(dataModel);
                                    } else {
                                        deferred.reject(data.errorMessage);
                                    }
                                    // 当相应准备就绪时调用
                                }).error(function (data, status, headers, config) {
                                    deferred.reject('http.status='+status);
                                    // 当响应以错误状态返回时调用
                                });
                                return deferred.promise;
                            },
                           deleteResource:function (resourceId){
                            var deferred = $q.defer();

                            $http({
                                method: 'POST',
                                url: resourceVisitAddress+"/"+resourceId,
                                data:{command:'DELETE'}
                            }).success(function (data, status, headers, config) {
                                if (data.status == 1) {
                                    var dataModel = data.result;
                                    deferred.resolve(dataModel);
                                } else {
                                    deferred.reject(data.errorMessage);
                                }
                                // 当相应准备就绪时调用
                            }).error(function (data, status, headers, config) {
                                deferred.reject('http.status='+status);
                                // 当响应以错误状态返回时调用
                            });
                            return deferred.promise;
                        }
                    }

                    }();
                };

            return this.resourceMapping[resource];
        }




    };


}]);